package com.health.mapp.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.commons.MD5Password;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.ImMsgNotice;
import com.health.db.model.ImMsgNoticeExample;
import com.health.db.model.UserLIST;
import com.health.db.service.msg.ImMsgNoticeService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.ModifyPasswordReq;
import com.health.mapp.bean.ModifyPasswordResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

public class ModifyPasswordImpl implements ActionInterface{
	private static Logger logger = Logger.getLogger(ModifyPasswordImpl.class);
	
	
	/* userId	String	Y	会员Id，operType=2时必送
    userPhone	String	N	会员账号，operType=1时必送
    oldPassword	String	N	老密码，operType=2时必送
    password	int	Y	新密码
    operType	int	Y	1：重置密码    2：修改密码*/
    
	private IStuffListService stuffListService;
	private IUserLISTService userLISTService  ;
	private ImMsgNoticeService imMsgNoticeService  ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		
		ModifyPasswordReq req = (ModifyPasswordReq) reqBean;
		ModifyPasswordResp _resp = new ModifyPasswordResp();
		String userId =req.getUserId();
	    String userPhone = req.getUserPhone();
	    String oldPassword =req.getOldPassword();
	    String operType = req.getOperType();
	    String password = req.getPassword();
	    String loginFlag = req.getLoginFlag();
	    
		UserLIST userList = new UserLIST();
		UserLIST userListUpdate = new UserLIST();
		userListUpdate.setUserNo(userPhone);
	
		//医生修改密码
		if (StringUtils.isNotBlank(loginFlag)&&("2").equals(loginFlag)) {
		
			if (("2").equals(operType)) {
			
				userList=userLISTService.selectByUserId(Long.valueOf(userId));
				userListUpdate.setUserid(userList.getUserid());
				userListUpdate.setPassword(userList.getPassword());
			}else if(("1").equals(operType)){
				
				ImMsgNoticeExample notice = new ImMsgNoticeExample();
				notice.setNoticeReceiveAddress(req.getUserPhone());
				notice.setIsUsedFlag((short) -1);
				notice.setCreateDate(DateUtils.getDateAfterSecond(DateUtils.now(),-5));
				StuffDetailDto stuffDetail = stuffListService.selectByStuffNo(userPhone);
				
				//导入用户不校验短信验证码
					
				List<ImMsgNotice> list=imMsgNoticeService.selectByExample(notice);
				if (list.size()==0) {
					_resp.setErrorCode(ResultCode.UN_VERIFY_SMS_ERROR);
					_resp.setErrorMsg(ResultCode.UN_VERIFY_SMS_ERROR_NOTE);
					return _resp;
				}
				
				userListUpdate.setUserid(stuffDetail.getStuffid());
				userListUpdate.setPassword(stuffDetail.getPassword());
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
		//用户修改密码	
		}else{
			if (("2").equals(operType)) {
				userList=userLISTService.selectByUserId(Long.valueOf(userId));
				userListUpdate.setUserid(userList.getUserid());
				userListUpdate.setPassword(userList.getPassword());
			}else if(("1").equals(operType)){
				//userList=userLISTService.selectByUserNo(userPhone);
				ImMsgNoticeExample notice = new ImMsgNoticeExample();
				notice.setNoticeReceiveAddress(req.getUserPhone());
				notice.setIsUsedFlag((short) -1);
				notice.setCreateDate(DateUtils.getDateAfterSecond(DateUtils.now(),-5));
				userList=userLISTService.selectByUserNo(userPhone);
				Short isExportFlag = userList.getIsExportFlag();
				
				//导入用户不校验短信验证码
				if (null!=isExportFlag&&isExportFlag.intValue()==2){
					
					List<ImMsgNotice> list=imMsgNoticeService.selectByExample(notice);
					if (list.size()==0) {
						_resp.setErrorCode(ResultCode.UN_VERIFY_SMS_ERROR);
						_resp.setErrorMsg(ResultCode.UN_VERIFY_SMS_ERROR_NOTE);
						return _resp;
					}
				}
				
				userListUpdate.setUserid(userList.getUserid());
				userListUpdate.setPassword(userList.getPassword());
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
		}
		
		if (null==userListUpdate) {
			_resp.setErrorCode(-1);
			_resp.setErrorMsg("查询不到相关用户信息");
		}else
		{
			if (("2").equals(operType)&&!userListUpdate.getPassword().startsWith("01000000")) {
				
				//密码校验
				if (!MD5Password.getEncodedPassword_Hex(oldPassword).equals(userListUpdate.getPassword())) {
					_resp.setErrorCode(ResultCode.LOGIN_FAIL_PASSWORD_IS_ERROR);
					_resp.setErrorMsg(ResultCode.LOGIN_FAIL_PASSWORD_IS_ERROR_NOTE);
					return _resp;
				}
			}
			
			userListUpdate.setPassword(MD5Password.getEncodedPassword_Hex(password));
			userListUpdate.setPasswordCreateDate(DateUtils.now());
			userLISTService.updatePassword(userListUpdate);
		
		}
		return _resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the stuffListService
	 */
	public IStuffListService getStuffListService() {
		return stuffListService;
	}
	/**
	 * @param stuffListService the stuffListService to set
	 */
	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}
	/**
	 * @return the imMsgNoticeService
	 */
	public ImMsgNoticeService getImMsgNoticeService() {
		return imMsgNoticeService;
	}
	/**
	 * @param imMsgNoticeService the imMsgNoticeService to set
	 */
	public void setImMsgNoticeService(ImMsgNoticeService imMsgNoticeService) {
		this.imMsgNoticeService = imMsgNoticeService;
	}
	
}
